package cn.codesea.housework_user.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.codesea.housework_user.R;
import cn.codesea.housework_user.adapter.listener.OnRecyclerItemClickServiceListener;
import cn.codesea.housework_user.adapter.SerListRcvAdapter;
import cn.codesea.housework_user.entity.Service;
import cn.codesea.housework_user.net.ServiceGenerator;
import cn.codesea.housework_user.net.data.Result;
import cn.codesea.housework_user.net.service.SerService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ServiceListActivity extends AppCompatActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    SerListRcvAdapter adapter;
    List<Service> services = new ArrayList<>();
    private LinearLayoutManager mLinearLayoutManager;

    private int service_id;
    private String title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_servicelist);
        ButterKnife.bind(this);
        init();
    }

    private void init(){
        initData();
        initView();
    }

    private void initData() {
        service_id = getIntent().getIntExtra("service_id",0);
        title = getIntent().getStringExtra("title");

        SerService serService = ServiceGenerator.createService(SerService.class);
        Call<Result> call = serService.getService(service_id);
        call.enqueue(new Callback<Result>(){
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                resultProcess(response.body());
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {

            }
        });
    }

    private void initView(){
        // 初始化 toolbar
        toolbar.setTitle(title);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        adapter = new SerListRcvAdapter(this,services);
        adapter.setRecyclerItemClickListener(new OnRecyclerItemClickServiceListener() {
            @Override
            public void onItemClick(int Position, List<Service> list) {
                if(5 == service_id){
                    startActivity(new Intent(ServiceListActivity.this,Service2Activity.class).putExtra("service",list.get(Position)));

                }else {
                    startActivity(new Intent(ServiceListActivity.this,ServiceActivity.class).putExtra("service",list.get(Position)));
                }
            }
        });
        mLinearLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        mRecyclerView.setLayoutManager(mLinearLayoutManager);
        mRecyclerView.setAdapter(adapter);
    }

    private void resultProcess(Result result){
        Type type = new TypeToken<List<Service>>(){}.getType();
        services.clear();
        services.addAll((List<Service>)new Gson().fromJson(String.valueOf(result.getRes()),type));
        adapter.notifyDataSetChanged();
    }
}
